package com.lam.dormitory.common.exception;

import com.lam.dormitory.util.ResponseTemplate;
import io.swagger.annotations.Api;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *  自定义异常
 */
@Api(tags = "异常处理")
@ControllerAdvice
public class MyShiroException {
    /**
     * 处理Shiro权限拦截异常
     * 如果返回JSON数据格式请加上 @ResponseBody注解
     */
    @ResponseBody
    @ExceptionHandler(value = AuthorizationException.class)
    public ResponseTemplate defaultErrorHandler(){
        return new ResponseTemplate(403,"权限不足");
    }

    @ResponseBody
    @ExceptionHandler(value = UnauthenticatedException.class)
    public ResponseTemplate UnauthenticatedException(){
        return new ResponseTemplate(402,"未登录");
    }


    @ResponseBody
    @ExceptionHandler(value = ExpiredCredentialsException.class)
    public ResponseTemplate ExpiredCredentialsException(){
        return new ResponseTemplate(401,"登录过期");
    }
}
